﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using System.Configuration;
using Com.Alipay;
using Com.Chuanglan;

namespace AnTeWei
{
    public class SMS
    {
        public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];

        public static int SendMessage(string strMobile,string strType)
        {
            String sendMessageResponse = String.Empty;
            String sendInternationalMessageResponse = String.Empty;

            int RandKey=new Random().Next(1000,9999);
            string account = "";
            string password = "";
            string mobile = strMobile;
            string content = "";

            try
            {
                //国内
                if (strType == "0")
                {
                    account = "vip-antewei";
                    password = "Antewei123";
                    content = "您本次的验证码是：" + RandKey + "此验证码仅用于安特威，若不是本人操作请忽略";
                    string postStrTpl = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true";

                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));

                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
                    myRequest.Method = "POST";
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.ContentLength = postData.Length;

                    Stream newStream = myRequest.GetRequestStream();
                    // Send the data.
                    newStream.Write(postData, 0, postData.Length);
                    newStream.Flush();
                    newStream.Close();

                    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                    if (myResponse.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                        return RandKey;
                    }
                    else
                    {
                        Log.Debug("短信接口回执", myResponse.StatusDescription);

                        return -1;
                    }
                    //Log.Debug("国内", strMobile);
                    //ChuanglanSMS clsCN = new ChuanglanSMS("vip-antewei", "Antewei123");
                    //content = "您本次的验证码是：" + RandKey + "此验证码仅用于安特威，若不是本人操作请忽略";
                    //sendMessageResponse = clsCN.sendMessage(strMobile, content);
                    //Log.Debug("SendMessage-国内", sendMessageResponse);
                    //if (sendMessageResponse == "DELIVRD")
                    //{
                    //    return RandKey;
                    //}
                    //else
                    //{
                    //    Log.Debug("SendMessage-国内", sendMessageResponse);
                    //    return -1;
                    //}
                }
                //国外
                else
                {
                    Log.Debug("国外", strMobile);
                    ChuanglanSMS clsInter = new ChuanglanSMS("I9003445", "3231c30c");
                    content = "您本次的验证码是：" + RandKey + "此验证码仅用于安特威，若不是本人操作请忽略";
                    sendInternationalMessageResponse = clsInter.sendInternationalMessage(strMobile, content);
                    Log.Debug("SendMessage-国外", sendInternationalMessageResponse);
                    if (sendInternationalMessageResponse == "DELIVRD")
                    {
                        return RandKey;
                    }
                    else
                    {
                        Log.Debug("SendMessage-国外", sendInternationalMessageResponse);
                        return -1;
                    }
                }

                #region 仅针对国内短信接口 弃用
                ////国内
                //if (strType == "0")
                //{
                //    Log.Debug("国内", strMobile);
                //    account = "vip-antewei";
                //    password = "Antewei123";
                //    content = "您本次的验证码是：" + RandKey + "此验证码仅用于安特威，若不是本人操作请忽略";
                //}
                ////国外
                //else
                //{
                //    Log.Debug("国外", strMobile);
                //    account = "vip-antewei";
                //    password = "Antewei123";
                //    content = "您本次的验证码是：" + RandKey + "此验证码仅用于安特威，若不是本人操作请忽略";
                //}



                //string postStrTpl = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true";

                //UTF8Encoding encoding = new UTF8Encoding();
                //byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));

                //HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
                //myRequest.Method = "POST";
                //myRequest.ContentType = "application/x-www-form-urlencoded";
                //myRequest.ContentLength = postData.Length;

                //Stream newStream = myRequest.GetRequestStream();
                //// Send the data.
                //newStream.Write(postData, 0, postData.Length);
                //newStream.Flush();
                //newStream.Close();

                //HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                //if (myResponse.StatusCode == HttpStatusCode.OK)
                //{
                //    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                //    return RandKey;
                //}
                //else
                //{
                //    Log.Debug("短信接口回执", myResponse.StatusDescription);

                //    return -1;
                //}
                #endregion
                
            }
            catch (Exception ex)
            {
                Log.Debug("短信接口异常", ex.Message.ToString());
                return -1;
            }
        }
    }
}